import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jinke/utils/util.dart';

class ImageGenerator {
  generate(String title) async {
    final recorder = ui.PictureRecorder();

    ui.Paint paint = new Paint()
      ..isAntiAlias = true
      ..filterQuality = ui.FilterQuality.high;
    double textMargin = 20.r; // 文字间间距，包括距离矩形边框左右间距
    // 获取标题高度等信息
    double textMaxWidth = 300.r;
    TextPainter titlePainter = new TextPainter(
        textScaleFactor: ScreenUtil().textScaleFactor,
        text: TextSpan(
          text: title,
          style: TextStyle(
            fontSize: 30.sp.toInt().toDouble(),
            color: Color(0xff333333),
          ),
        ),
        textDirection: TextDirection.ltr)
      ..layout(maxWidth: textMaxWidth);
    var titleHeight = titlePainter.height;
    var titleWidth = titlePainter.width;
    print("titleHeight =  $titleHeight");

    double textHeight = (titleHeight + 2 * textMargin);
    double textWidth = (titleWidth + 2 * textMargin);

    // 利用矩形左边的X坐标、矩形顶部的Y坐标、矩形右边的X坐标、矩形底部的Y坐标确定矩形的大小和位置
    var canvasRect = Rect.fromLTWH(0, 0, textWidth, textHeight);
    final canvas = Canvas(recorder, canvasRect);
    // 0. 绘制背景
    canvas.drawColor(Colors.transparent, BlendMode.color);

    // 2. 绘制矩形，先绘制矩形，否则文字被覆盖
    paint.color = Colors.white;
    var rrect = RRect.fromRectAndRadius(
        Rect.fromLTWH(0, 0, textWidth, textHeight), Radius.circular(6));

    canvas.drawRRect(rrect, paint);

    double trangleWidth = 40.r;
    Path path = Path()
      ..moveTo(textWidth / 2 - trangleWidth / 2, textHeight + 10.r);
    path.lineTo(textWidth / 2 + trangleWidth / 2, textHeight + 10.r);
    path.lineTo(textWidth / 2, textHeight + trangleWidth * 0.8);
    canvas.drawPath(
        path,
        paint
          ..style = PaintingStyle.fill
          ..color = Colors.white);

    double bottom = textHeight + trangleWidth;
    print("bottom = $bottom");
    // canvas.drawLine(
    //     ui.Offset(
    //         textWidth / 2 - trangleWidth / 2, textHeight + trangleWidth / 2),
    //     ui.Offset(
    //         textWidth / 2 + trangleWidth / 2, textHeight + trangleWidth / 2),
    //     paint);
    // canvas.drawLine(
    //     ui.Offset(
    //         textWidth / 2 - trangleWidth / 2, textHeight + trangleWidth / 2),
    //     ui.Offset(
    //         textWidth / 2, textHeight + trangleWidth / 2 + trangleWidth * 0.8),
    //     paint);
    // canvas.drawLine(
    //     ui.Offset(
    //         textWidth / 2 + trangleWidth / 2, textHeight + trangleWidth / 2),
    //     ui.Offset(
    //         textWidth / 2, textHeight + trangleWidth / 2 + trangleWidth * 0.8),
    //     paint);
    // 3. 绘制文字
    titlePainter.paint(canvas, Offset(textMargin, textMargin));

    // 转换成图片
    final picture = recorder.endRecording();

    ui.Image img = await picture.toImage(textWidth.toInt(), bottom.toInt());

    print('img的尺寸: $img');
    ByteData? _data = await img.toByteData(format: ui.ImageByteFormat.png);
    var _icondata = _data?.buffer.asUint8List();
    return _icondata;
  }
}

// class ImageGenerator {
//   generate(String title) async {
//     final recorder = ui.PictureRecorder();

//     ui.Paint paint = new Paint()
//       ..isAntiAlias = true
//       ..filterQuality = ui.FilterQuality.high;
//     double rectTextTop = 0; // 文本显示矩形顶部距离图片最顶部的距离
//     double textMargin = 5; // 文字间间距，包括距离矩形边框左右间距
//     double pagePadding = 0; // 页面内容左右边距
//     double bottomHeight = 0; // 底部区域高度
//     // 获取标题高度等信息
//     double textMaxWidth = 100;
//     TextPainter titlePainter = new TextPainter(
//         text: TextSpan(
//           text: title,
//           style: TextStyle(
//             fontSize: 13,
//             color: Colors.white,
//           ),
//         ),
//         textDirection: TextDirection.ltr)
//       ..layout(maxWidth: textMaxWidth);
//     var titleHeight = titlePainter.height;
//     var titleWidth = titlePainter.width;
//     print("titleHeight =  $titleHeight");

//     double textHeight = titleHeight + 2 * textMargin;
//     double bottom = textHeight + rectTextTop + textMargin * 2 + bottomHeight;
//     double shadowBottom = textHeight + rectTextTop;
//     print("bottom = $bottom");

//     // 利用矩形左边的X坐标、矩形顶部的Y坐标、矩形右边的X坐标、矩形底部的Y坐标确定矩形的大小和位置
//     var canvasRect = Rect.fromLTWH(0, 0, titleWidth, bottom);
//     final canvas = Canvas(recorder, canvasRect);
//     // 0. 绘制背景
//     canvas.drawColor(Colors.black38, BlendMode.color);

//     // 2. 绘制矩形，先绘制矩形，否则文字被覆盖
//     paint.color = Colors.red;
//     var rrect = RRect.fromRectAndRadius(
//         Rect.fromLTWH(
//             pagePadding, rectTextTop, titleWidth - pagePadding * 2, textHeight),
//         Radius.circular(6));

//     var path = Path()
//       ..moveTo(pagePadding, rectTextTop)
//       ..lineTo(titleWidth - pagePadding, rectTextTop)
//       ..lineTo(titleWidth - pagePadding, shadowBottom)
//       ..lineTo(pagePadding, shadowBottom)
//       ..close();
//     canvas.drawRRect(rrect, paint);

//     // 3. 绘制文字
//     titlePainter.paint(
//         canvas, Offset(pagePadding + textMargin, rectTextTop + textMargin));

//     // 转换成图片
//     final picture = recorder.endRecording();

//     ui.Image img = await picture.toImage(titleWidth.toInt(), bottom.toInt());

//     print('img的尺寸: $img');
//     ByteData? _data = await img.toByteData();
//     var _icondata = _data?.buffer.asUint8List();
//     return _data?.buffer;
//   }
// }
